<?php

declare(strict_types=1);

namespace app\validate\verify;

use think\Validate;

class FutureBuyerCreateValidate extends Validate
{
    protected $rule = [
        'product_id' => 'requireWithout:prod_id|integer|gt:0',
        'prod_id' => 'requireWithout:product_id|integer|gt:0',
        'sku_id' => 'integer|gt:0',
        'buyer_qty' => 'require|integer|gt:0',
        'buyer_expected_price' => 'require|float|gt:0',
        'buyer_expected_date' => 'date',
        'buyer_remark' => 'max:255',
        'buyer_media' => 'checkMedia',
    ];

    protected $message = [
        'product_id.requireWithout' => '商品ID不能为空',
        'product_id.integer' => '商品ID格式不正确',
        'product_id.gt' => '商品ID格式不正确',
        'prod_id.requireWithout' => '商品ID不能为空',
        'prod_id.integer' => '商品ID格式不正确',
        'prod_id.gt' => '商品ID格式不正确',
        'sku_id.integer' => 'SKU格式不正确',
        'sku_id.gt' => 'SKU格式不正确',
        'buyer_qty.require' => '需求数量不能为空',
        'buyer_qty.integer' => '需求数量必须是整数',
        'buyer_qty.gt' => '需求数量必须大于0',
        'buyer_expected_price.require' => '期望价格不能为空',
        'buyer_expected_price.float' => '期望价格格式不正确',
        'buyer_expected_price.gt' => '期望价格必须大于0',
        'buyer_expected_date.date' => '期望交期格式不正确',
        'buyer_remark.max' => '买家备注不能超过255个字符',
    ];

    protected $scene = [
        'create' => ['product_id', 'prod_id', 'sku_id', 'buyer_qty', 'buyer_expected_price', 'buyer_expected_date', 'buyer_remark', 'buyer_media'],
    ];

    protected function checkMedia($value)
    {
        if ($value === null || $value === '') {
            return true;
        }

        if (!is_array($value) && !is_string($value)) {
            return '附件格式不正确';
        }

        return true;
    }
}
